/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Rol;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Rolmodulo;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Rolareamodulo;
import Entidades.Cargo;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class RolJpaController {

    public RolJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Rol rol) {
        if (rol.getRolmoduloCollection() == null) {
            rol.setRolmoduloCollection(new ArrayList<Rolmodulo>());
        }
        if (rol.getRolareamoduloCollection() == null) {
            rol.setRolareamoduloCollection(new ArrayList<Rolareamodulo>());
        }
        if (rol.getCargoCollection() == null) {
            rol.setCargoCollection(new ArrayList<Cargo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Rolmodulo> attachedRolmoduloCollection = new ArrayList<Rolmodulo>();
            for (Rolmodulo rolmoduloCollectionRolmoduloToAttach : rol.getRolmoduloCollection()) {
                rolmoduloCollectionRolmoduloToAttach = em.getReference(rolmoduloCollectionRolmoduloToAttach.getClass(), rolmoduloCollectionRolmoduloToAttach.getIdRolModulo());
                attachedRolmoduloCollection.add(rolmoduloCollectionRolmoduloToAttach);
            }
            rol.setRolmoduloCollection(attachedRolmoduloCollection);
            Collection<Rolareamodulo> attachedRolareamoduloCollection = new ArrayList<Rolareamodulo>();
            for (Rolareamodulo rolareamoduloCollectionRolareamoduloToAttach : rol.getRolareamoduloCollection()) {
                rolareamoduloCollectionRolareamoduloToAttach = em.getReference(rolareamoduloCollectionRolareamoduloToAttach.getClass(), rolareamoduloCollectionRolareamoduloToAttach.getIdRolAreaModulo());
                attachedRolareamoduloCollection.add(rolareamoduloCollectionRolareamoduloToAttach);
            }
            rol.setRolareamoduloCollection(attachedRolareamoduloCollection);
            Collection<Cargo> attachedCargoCollection = new ArrayList<Cargo>();
            for (Cargo cargoCollectionCargoToAttach : rol.getCargoCollection()) {
                cargoCollectionCargoToAttach = em.getReference(cargoCollectionCargoToAttach.getClass(), cargoCollectionCargoToAttach.getIdCargo());
                attachedCargoCollection.add(cargoCollectionCargoToAttach);
            }
            rol.setCargoCollection(attachedCargoCollection);
            em.persist(rol);
            for (Rolmodulo rolmoduloCollectionRolmodulo : rol.getRolmoduloCollection()) {
                Rol oldRolOfRolmoduloCollectionRolmodulo = rolmoduloCollectionRolmodulo.getRol();
                rolmoduloCollectionRolmodulo.setRol(rol);
                rolmoduloCollectionRolmodulo = em.merge(rolmoduloCollectionRolmodulo);
                if (oldRolOfRolmoduloCollectionRolmodulo != null) {
                    oldRolOfRolmoduloCollectionRolmodulo.getRolmoduloCollection().remove(rolmoduloCollectionRolmodulo);
                    oldRolOfRolmoduloCollectionRolmodulo = em.merge(oldRolOfRolmoduloCollectionRolmodulo);
                }
            }
            for (Rolareamodulo rolareamoduloCollectionRolareamodulo : rol.getRolareamoduloCollection()) {
                Rol oldRolOfRolareamoduloCollectionRolareamodulo = rolareamoduloCollectionRolareamodulo.getRol();
                rolareamoduloCollectionRolareamodulo.setRol(rol);
                rolareamoduloCollectionRolareamodulo = em.merge(rolareamoduloCollectionRolareamodulo);
                if (oldRolOfRolareamoduloCollectionRolareamodulo != null) {
                    oldRolOfRolareamoduloCollectionRolareamodulo.getRolareamoduloCollection().remove(rolareamoduloCollectionRolareamodulo);
                    oldRolOfRolareamoduloCollectionRolareamodulo = em.merge(oldRolOfRolareamoduloCollectionRolareamodulo);
                }
            }
            for (Cargo cargoCollectionCargo : rol.getCargoCollection()) {
                Rol oldRolOfCargoCollectionCargo = cargoCollectionCargo.getRol();
                cargoCollectionCargo.setRol(rol);
                cargoCollectionCargo = em.merge(cargoCollectionCargo);
                if (oldRolOfCargoCollectionCargo != null) {
                    oldRolOfCargoCollectionCargo.getCargoCollection().remove(cargoCollectionCargo);
                    oldRolOfCargoCollectionCargo = em.merge(oldRolOfCargoCollectionCargo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Rol rol) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Rol persistentRol = em.find(Rol.class, rol.getIdRol());
            Collection<Rolmodulo> rolmoduloCollectionOld = persistentRol.getRolmoduloCollection();
            Collection<Rolmodulo> rolmoduloCollectionNew = rol.getRolmoduloCollection();
            Collection<Rolareamodulo> rolareamoduloCollectionOld = persistentRol.getRolareamoduloCollection();
            Collection<Rolareamodulo> rolareamoduloCollectionNew = rol.getRolareamoduloCollection();
            Collection<Cargo> cargoCollectionOld = persistentRol.getCargoCollection();
            Collection<Cargo> cargoCollectionNew = rol.getCargoCollection();
            List<String> illegalOrphanMessages = null;
            for (Rolmodulo rolmoduloCollectionOldRolmodulo : rolmoduloCollectionOld) {
                if (!rolmoduloCollectionNew.contains(rolmoduloCollectionOldRolmodulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rolmodulo " + rolmoduloCollectionOldRolmodulo + " since its rol field is not nullable.");
                }
            }
            for (Rolareamodulo rolareamoduloCollectionOldRolareamodulo : rolareamoduloCollectionOld) {
                if (!rolareamoduloCollectionNew.contains(rolareamoduloCollectionOldRolareamodulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rolareamodulo " + rolareamoduloCollectionOldRolareamodulo + " since its rol field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Rolmodulo> attachedRolmoduloCollectionNew = new ArrayList<Rolmodulo>();
            for (Rolmodulo rolmoduloCollectionNewRolmoduloToAttach : rolmoduloCollectionNew) {
                rolmoduloCollectionNewRolmoduloToAttach = em.getReference(rolmoduloCollectionNewRolmoduloToAttach.getClass(), rolmoduloCollectionNewRolmoduloToAttach.getIdRolModulo());
                attachedRolmoduloCollectionNew.add(rolmoduloCollectionNewRolmoduloToAttach);
            }
            rolmoduloCollectionNew = attachedRolmoduloCollectionNew;
            rol.setRolmoduloCollection(rolmoduloCollectionNew);
            Collection<Rolareamodulo> attachedRolareamoduloCollectionNew = new ArrayList<Rolareamodulo>();
            for (Rolareamodulo rolareamoduloCollectionNewRolareamoduloToAttach : rolareamoduloCollectionNew) {
                rolareamoduloCollectionNewRolareamoduloToAttach = em.getReference(rolareamoduloCollectionNewRolareamoduloToAttach.getClass(), rolareamoduloCollectionNewRolareamoduloToAttach.getIdRolAreaModulo());
                attachedRolareamoduloCollectionNew.add(rolareamoduloCollectionNewRolareamoduloToAttach);
            }
            rolareamoduloCollectionNew = attachedRolareamoduloCollectionNew;
            rol.setRolareamoduloCollection(rolareamoduloCollectionNew);
            Collection<Cargo> attachedCargoCollectionNew = new ArrayList<Cargo>();
            for (Cargo cargoCollectionNewCargoToAttach : cargoCollectionNew) {
                cargoCollectionNewCargoToAttach = em.getReference(cargoCollectionNewCargoToAttach.getClass(), cargoCollectionNewCargoToAttach.getIdCargo());
                attachedCargoCollectionNew.add(cargoCollectionNewCargoToAttach);
            }
            cargoCollectionNew = attachedCargoCollectionNew;
            rol.setCargoCollection(cargoCollectionNew);
            rol = em.merge(rol);
            for (Rolmodulo rolmoduloCollectionNewRolmodulo : rolmoduloCollectionNew) {
                if (!rolmoduloCollectionOld.contains(rolmoduloCollectionNewRolmodulo)) {
                    Rol oldRolOfRolmoduloCollectionNewRolmodulo = rolmoduloCollectionNewRolmodulo.getRol();
                    rolmoduloCollectionNewRolmodulo.setRol(rol);
                    rolmoduloCollectionNewRolmodulo = em.merge(rolmoduloCollectionNewRolmodulo);
                    if (oldRolOfRolmoduloCollectionNewRolmodulo != null && !oldRolOfRolmoduloCollectionNewRolmodulo.equals(rol)) {
                        oldRolOfRolmoduloCollectionNewRolmodulo.getRolmoduloCollection().remove(rolmoduloCollectionNewRolmodulo);
                        oldRolOfRolmoduloCollectionNewRolmodulo = em.merge(oldRolOfRolmoduloCollectionNewRolmodulo);
                    }
                }
            }
            for (Rolareamodulo rolareamoduloCollectionNewRolareamodulo : rolareamoduloCollectionNew) {
                if (!rolareamoduloCollectionOld.contains(rolareamoduloCollectionNewRolareamodulo)) {
                    Rol oldRolOfRolareamoduloCollectionNewRolareamodulo = rolareamoduloCollectionNewRolareamodulo.getRol();
                    rolareamoduloCollectionNewRolareamodulo.setRol(rol);
                    rolareamoduloCollectionNewRolareamodulo = em.merge(rolareamoduloCollectionNewRolareamodulo);
                    if (oldRolOfRolareamoduloCollectionNewRolareamodulo != null && !oldRolOfRolareamoduloCollectionNewRolareamodulo.equals(rol)) {
                        oldRolOfRolareamoduloCollectionNewRolareamodulo.getRolareamoduloCollection().remove(rolareamoduloCollectionNewRolareamodulo);
                        oldRolOfRolareamoduloCollectionNewRolareamodulo = em.merge(oldRolOfRolareamoduloCollectionNewRolareamodulo);
                    }
                }
            }
            for (Cargo cargoCollectionOldCargo : cargoCollectionOld) {
                if (!cargoCollectionNew.contains(cargoCollectionOldCargo)) {
                    cargoCollectionOldCargo.setRol(null);
                    cargoCollectionOldCargo = em.merge(cargoCollectionOldCargo);
                }
            }
            for (Cargo cargoCollectionNewCargo : cargoCollectionNew) {
                if (!cargoCollectionOld.contains(cargoCollectionNewCargo)) {
                    Rol oldRolOfCargoCollectionNewCargo = cargoCollectionNewCargo.getRol();
                    cargoCollectionNewCargo.setRol(rol);
                    cargoCollectionNewCargo = em.merge(cargoCollectionNewCargo);
                    if (oldRolOfCargoCollectionNewCargo != null && !oldRolOfCargoCollectionNewCargo.equals(rol)) {
                        oldRolOfCargoCollectionNewCargo.getCargoCollection().remove(cargoCollectionNewCargo);
                        oldRolOfCargoCollectionNewCargo = em.merge(oldRolOfCargoCollectionNewCargo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = rol.getIdRol();
                if (findRol(id) == null) {
                    throw new NonexistentEntityException("The rol with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Rol rol;
            try {
                rol = em.getReference(Rol.class, id);
                rol.getIdRol();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The rol with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Rolmodulo> rolmoduloCollectionOrphanCheck = rol.getRolmoduloCollection();
            for (Rolmodulo rolmoduloCollectionOrphanCheckRolmodulo : rolmoduloCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Rol (" + rol + ") cannot be destroyed since the Rolmodulo " + rolmoduloCollectionOrphanCheckRolmodulo + " in its rolmoduloCollection field has a non-nullable rol field.");
            }
            Collection<Rolareamodulo> rolareamoduloCollectionOrphanCheck = rol.getRolareamoduloCollection();
            for (Rolareamodulo rolareamoduloCollectionOrphanCheckRolareamodulo : rolareamoduloCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Rol (" + rol + ") cannot be destroyed since the Rolareamodulo " + rolareamoduloCollectionOrphanCheckRolareamodulo + " in its rolareamoduloCollection field has a non-nullable rol field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Cargo> cargoCollection = rol.getCargoCollection();
            for (Cargo cargoCollectionCargo : cargoCollection) {
                cargoCollectionCargo.setRol(null);
                cargoCollectionCargo = em.merge(cargoCollectionCargo);
            }
            em.remove(rol);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Rol> findRolEntities() {
        return findRolEntities(true, -1, -1);
    }

    public List<Rol> findRolEntities(int maxResults, int firstResult) {
        return findRolEntities(false, maxResults, firstResult);
    }

    private List<Rol> findRolEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Rol as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Rol findRol(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Rol.class, id);
        } finally {
            em.close();
        }
    }

    public int getRolCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Rol as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public Rol findRol_x_nombre(String nom) {
           EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Rol u where u.nombreRol = " + "'" + nom +"'");
            return (Rol) q.getSingleResult();
        } finally {
            em.close();
}
    }

}
